<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QThread Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QThread Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QThread class provides platform-independent threads.
<a href="#details">More...</a></p>

<p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qthread.html#Priority-enum">Priority</a></b> { IdlePriority, LowestPriority, LowPriority, NormalPriority, ..., InheritPriority }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qthread.html#QThread">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" />int <b><a href="qthread.html#exec">exec_</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qthread.html#exit">exit</a></b> (<i>self</i>, int&#160;<i>returnCode</i>&#160;=&#160;0)</li><li><div class="fn" />bool <b><a href="qthread.html#isFinished">isFinished</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qthread.html#isRunning">isRunning</a></b> (<i>self</i>)</li><li><div class="fn" />Priority <b><a href="qthread.html#priority">priority</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qthread.html#quit">quit</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qthread.html#run">run</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qthread.html#setPriority">setPriority</a></b> (<i>self</i>, Priority&#160;<i>priority</i>)</li><li><div class="fn" /><b><a href="qthread.html#setStackSize">setStackSize</a></b> (<i>self</i>, int&#160;<i>stackSize</i>)</li><li><div class="fn" />int <b><a href="qthread.html#stackSize">stackSize</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qthread.html#start">start</a></b> (<i>self</i>, Priority&#160;<i>priority</i>&#160;=&#160;QThread.InheritPriority)</li><li><div class="fn" /><b><a href="qthread.html#terminate">terminate</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qthread.html#wait">wait</a></b> (<i>self</i>, int&#160;<i>msecs</i>&#160;=&#160;ULONG_MAX)</li></ul><h3>Static Methods</h3><ul><li><div class="fn" />QThread <b><a href="qthread.html#currentThread">currentThread</a></b> ()</li><li><div class="fn" />int <b><a href="qthread.html#currentThreadId">currentThreadId</a></b> ()</li><li><div class="fn" />int <b><a href="qthread.html#idealThreadCount">idealThreadCount</a></b> ()</li><li><div class="fn" /><b><a href="qthread.html#msleep">msleep</a></b> (int)</li><li><div class="fn" /><b><a href="qthread.html#setTerminationEnabled">setTerminationEnabled</a></b> (bool&#160;<i>enabled</i>&#160;=&#160;True)</li><li><div class="fn" /><b><a href="qthread.html#sleep">sleep</a></b> (int)</li><li><div class="fn" /><b><a href="qthread.html#usleep">usleep</a></b> (int)</li><li><div class="fn" /><b><a href="qthread.html#yieldCurrentThread">yieldCurrentThread</a></b> ()</li></ul><h3>Qt Signals</h3><ul><li><div class="fn" />void <b><a href="qthread.html#finished">finished</a></b> ()</li><li><div class="fn" />void <b><a href="qthread.html#started">started</a></b> ()</li><li><div class="fn" />void <b><a href="qthread.html#terminated">terminated</a></b> ()</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QThread class provides platform-independent threads.</p>
<p>A QThread represents a separate thread of control within the
program; it shares data with all the other threads within the
process but executes independently in the way that a separate
program does on a multitasking operating system. Instead of
starting in <tt>main()</tt>, QThreads begin executing in <a href="qthread.html#run">run</a>(). By default, <a href="qthread.html#run">run</a>() starts the event loop by calling
<a href="qthread.html#exec">exec_</a>() (see below). To create your
own threads, subclass QThread and reimplement <a href="qthread.html#run">run</a>(). For example:</p>
<pre class="cpp">
 <span class="keyword">class</span> MyThread : <span class="keyword">public</span> <span class="type">QThread</span>
 {
 <span class="keyword">public</span>:
     <span class="type">void</span> run();
 };

 <span class="type">void</span> MyThread<span class="operator">.</span><a href="qthread.html#run">run</a>()
 {
     <span class="type"><a href="qtcpsocket.html">QTcpSocket</a></span> socket;
     <span class="comment">// connect QTcpSocket's signals somewhere meaningful</span>
     <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
     socket<span class="operator">.</span>connectToHost(hostName<span class="operator">,</span> portNumber);
     exec();
 }
</pre>
<p>This will create a <a href="qtcpsocket.html">QTcpSocket</a> in
the thread and then execute the thread's event loop. Use the
<a href="qthread.html#start">start</a>() method to begin execution.
Execution ends when you return from <a href="qthread.html#run">run</a>(), just as an application does when it
leaves main(). QThread will notifiy you via a signal when the
thread is <a href="qthread.html#started">started</a>(), <a href="qthread.html#finished">finished</a>(), and <a href="qthread.html#terminated">terminated</a>(), or you can use <a href="qthread.html#isFinished">isFinished</a>() and <a href="qthread.html#isRunning">isRunning</a>() to query the state of the
thread. Use <a href="qthread.html#wait">wait</a>() to block until
the thread has finished execution.</p>
<p>Each thread gets its own stack from the operating system. The
operating system also determines the default size of the stack. You
can use <a href="qthread.html#setStackSize">setStackSize</a>() to
set a custom stack size.</p>
<p>Each QThread can have its own event loop. You can start the
event loop by calling <a href="qthread.html#exec">exec_</a>(); you
can stop it by calling <a href="qthread.html#exit">exit</a>() or
<a href="qthread.html#quit">quit</a>(). Having an event loop in a
thread makes it possible to connect signals from other threads to
slots in this thread, using a mechanism called <a href="qt.html#ConnectionType-enum">queued connections</a>. It also makes
it possible to use classes that require the event loop, such as
<a href="qtimer.html">QTimer</a> and <a href="qtcpsocket.html">QTcpSocket</a>, in the thread. Note, however,
that it is not possible to use any widget classes in the
thread.</p>
<p>In extreme cases, you may want to forcibly <a href="qthread.html#terminate">terminate</a>() an executing thread.
However, doing so is dangerous and discouraged. Please read the
documentation for <a href="qthread.html#terminate">terminate</a>()
and <a href="qthread.html#setTerminationEnabled">setTerminationEnabled</a>()
for detailed information.</p>
<p>The static functions <a href="qthread.html#currentThreadId">currentThreadId</a>() and <a href="qthread.html#currentThread">currentThread</a>() return identifiers
for the currently executing thread. The former returns a platform
specific ID for the thread; the latter returns a QThread
pointer.</p>
<p>QThread also provides platform independent sleep functions in
varying resolutions. Use <a href="qthread.html#sleep">sleep</a>()
for full second resolution, <a href="qthread.html#msleep">msleep</a>() for millisecond resolution, and
<a href="qthread.html#usleep">usleep</a>() for microsecond
resolution.</p>
<hr /><h2>Type Documentation</h2><h3 class="fn"><a name="Priority-enum" />QThread.Priority</h3><p>This enum type indicates how the operating system should
schedule newly created threads.</p>
<table class="valuelist">
<tr class="odd" valign="top">
<th class="tblConst">Constant</th>
<th class="tblval">Value</th>
<th class="tbldscr">Description</th>
</tr>
<tr>
<td class="topAlign"><tt>QThread.IdlePriority</tt></td>
<td class="topAlign"><tt>0</tt></td>
<td class="topAlign">scheduled only when no other threads are
running.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.LowestPriority</tt></td>
<td class="topAlign"><tt>1</tt></td>
<td class="topAlign">scheduled less often than LowPriority.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.LowPriority</tt></td>
<td class="topAlign"><tt>2</tt></td>
<td class="topAlign">scheduled less often than NormalPriority.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.NormalPriority</tt></td>
<td class="topAlign"><tt>3</tt></td>
<td class="topAlign">the default priority of the operating
system.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.HighPriority</tt></td>
<td class="topAlign"><tt>4</tt></td>
<td class="topAlign">scheduled more often than NormalPriority.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.HighestPriority</tt></td>
<td class="topAlign"><tt>5</tt></td>
<td class="topAlign">scheduled more often than HighPriority.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.TimeCriticalPriority</tt></td>
<td class="topAlign"><tt>6</tt></td>
<td class="topAlign">scheduled as often as possible.</td>
</tr>
<tr>
<td class="topAlign"><tt>QThread.InheritPriority</tt></td>
<td class="topAlign"><tt>7</tt></td>
<td class="topAlign">use the same priority as the creating thread.
This is the default.</td>
</tr>
</table>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QThread" />QThread.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a new thread with the given <i>parent</i>. The thread
does not begin executing until <a href="qthread.html#start">start</a>() is called.</p>
<p><b>See also</b> <a href="qthread.html#start">start</a>().</p>


<h3 class="fn"><a name="currentThread" /><a href="qthread.html">QThread</a> QThread.currentThread ()</h3><p>Returns a pointer to a <a href="qthread.html">QThread</a> which
represents the currently executing thread.</p>


<h3 class="fn"><a name="currentThreadId" />int QThread.currentThreadId ()</h3><p>Returns the thread handle of the currently executing thread.</p>
<p><b>Warning:</b> The handle returned by this function is used for
internal purposes and should not be used in any application
code.</p>
<p><b>Warning:</b> On Windows, the returned value is a
pseudo-handle for the current thread. It can't be used for
numerical comparison. i.e., this function returns the DWORD
(Windows-Thread ID) returned by the Win32 function
getCurrentThreadId(), not the HANDLE (Windows-Thread HANDLE)
returned by the Win32 function getCurrentThread().</p>


<h3 class="fn"><a name="exec" />int QThread.exec_ (<i>self</i>)</h3><p>Enters the event loop and waits until <a href="qthread.html#exit">exit</a>() is called, returning the value that
was passed to <a href="qthread.html#exit">exit</a>(). The value
returned is 0 if <a href="qthread.html#exit">exit</a>() is called
via <a href="qthread.html#quit">quit</a>().</p>
<p>It is necessary to call this function to start event
handling.</p>
<p><b>See also</b> <a href="qthread.html#quit">quit</a>() and
<a href="qthread.html#exit">exit</a>().</p>


<h3 class="fn"><a name="exit" />QThread.exit (<i>self</i>, int&#160;<i>returnCode</i>&#160;=&#160;0)</h3><p>Tells the thread's event loop to exit with a return code.</p>
<p>After calling this function, the thread leaves the event loop
and returns from the call to <a href="qeventloop.html#exec">QEventLoop.exec</a>(). The <a href="qeventloop.html#exec">QEventLoop.exec</a>() function returns
<i>returnCode</i>.</p>
<p>By convention, a <i>returnCode</i> of 0 means success, any
non-zero value indicates an error.</p>
<p>Note that unlike the C library function of the same name, this
function <i>does</i> return to the caller -- it is event processing
that stops.</p>
<p>No QEventLoops will be started anymore in this thread until
<a href="qthread.html#exec">QThread.exec</a>() has been called
again. If the eventloop in <a href="qthread.html#exec">QThread.exec</a>() is not running then the
next call to <a href="qthread.html#exec">QThread.exec</a>() will
also return immediately.</p>
<p><b>See also</b> <a href="qthread.html#quit">quit</a>() and
<a href="qeventloop.html">QEventLoop</a>.</p>


<h3 class="fn"><a name="idealThreadCount" />int QThread.idealThreadCount ()</h3><p>Returns the ideal number of threads that can be run on the
system. This is done querying the number of processor cores, both
real and logical, in the system. This function returns -1 if the
number of processor cores could not be detected.</p>


<h3 class="fn"><a name="isFinished" />bool QThread.isFinished (<i>self</i>)</h3><p>Returns true if the thread is finished; otherwise returns
false.</p>
<p><b>See also</b> <a href="qthread.html#isRunning">isRunning</a>().</p>


<h3 class="fn"><a name="isRunning" />bool QThread.isRunning (<i>self</i>)</h3><p>Returns true if the thread is running; otherwise returns
false.</p>
<p><b>See also</b> <a href="qthread.html#isFinished">isFinished</a>().</p>


<h3 class="fn"><a name="msleep" />QThread.msleep (int)</h3><p>Causes the current thread to sleep for <i>msecs</i>
milliseconds.</p>
<p><b>See also</b> <a href="qthread.html#sleep">sleep</a>() and
<a href="qthread.html#usleep">usleep</a>().</p>


<h3 class="fn"><a name="priority" /><a href="qthread.html#Priority-enum">Priority</a> QThread.priority (<i>self</i>)</h3><p>Returns the priority for a running thread. If the thread is not
running, this function returns <tt>InheritPriority</tt>.</p>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qthread.html#Priority-enum">Priority</a>, <a href="qthread.html#setPriority">setPriority</a>(), and <a href="qthread.html#start">start</a>().</p>


<h3 class="fn"><a name="quit" />QThread.quit (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void quit()</tt>.</p><p>Tells the thread's event loop to exit with return code 0
(success). Equivalent to calling QThread.exit(0).</p>
<p>This function does nothing if the thread does not have an event
loop.</p>
<p><b>See also</b> <a href="qthread.html#exit">exit</a>() and
<a href="qeventloop.html">QEventLoop</a>.</p>


<h3 class="fn"><a name="run" />QThread.run (<i>self</i>)</h3><p>The starting point for the thread. After calling <a href="qthread.html#start">start</a>(), the newly created thread calls
this function. The default implementation simply calls <a href="qthread.html#exec">exec_</a>().</p>
<p>You can reimplemented this function to do other useful work.
Returning from this method will end the execution of the
thread.</p>
<p><b>See also</b> <a href="qthread.html#start">start</a>() and
<a href="qthread.html#wait">wait</a>().</p>


<h3 class="fn"><a name="setPriority" />QThread.setPriority (<i>self</i>, <a href="qthread.html#Priority-enum">Priority</a>&#160;<i>priority</i>)</h3><p>This function sets the <i>priority</i> for a running thread. If
the thread is not running, this function does nothing and returns
immediately. Use <a href="qthread.html#start">start</a>() to start
a thread with a specific priority.</p>
<p>The <i>priority</i> argument can be any value in the
<tt>QThread.Priority</tt> enum except for
<tt>InheritPriorty</tt>.</p>
<p>The effect of the <i>priority</i> parameter is dependent on the
operating system's scheduling policy. In particular, the
<i>priority</i> will be ignored on systems that do not support
thread priorities (such as on Linux, see
http://linux.die.net/man/2/sched_setscheduler for more
details).</p>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qthread.html#Priority-enum">Priority</a>, <a href="qthread.html#priority">priority</a>(), and <a href="qthread.html#start">start</a>().</p>


<h3 class="fn"><a name="setStackSize" />QThread.setStackSize (<i>self</i>, int&#160;<i>stackSize</i>)</h3><p>Sets the maximum stack size for the thread to <i>stackSize</i>.
If <i>stackSize</i> is greater than zero, the maximum stack size is
set to <i>stackSize</i> bytes, otherwise the maximum stack size is
automatically determined by the operating system.</p>
<p><b>Warning:</b> Most operating systems place minimum and maximum
limits on thread stack sizes. The thread will fail to start if the
stack size is outside these limits.</p>
<p><b>See also</b> <a href="qthread.html#stackSize">stackSize</a>().</p>


<h3 class="fn"><a name="setTerminationEnabled" />QThread.setTerminationEnabled (bool&#160;<i>enabled</i>&#160;=&#160;True)</h3><p>Enables or disables termination of the current thread based on
the <i>enabled</i> parameter. The thread must have been started by
<a href="qthread.html">QThread</a>.</p>
<p>When <i>enabled</i> is false, termination is disabled. Future
calls to <a href="qthread.html#terminate">QThread.terminate</a>()
will return immediately without effect. Instead, the termination is
deferred until termination is enabled.</p>
<p>When <i>enabled</i> is true, termination is enabled. Future
calls to <a href="qthread.html#terminate">QThread.terminate</a>()
will terminate the thread normally. If termination has been
deferred (i.e. <a href="qthread.html#terminate">QThread.terminate</a>() was called with
termination disabled), this function will terminate the calling
thread <i>immediately</i>. Note that this function will not return
in this case.</p>
<p><b>See also</b> <a href="qthread.html#terminate">terminate</a>().</p>


<h3 class="fn"><a name="sleep" />QThread.sleep (int)</h3><p>Forces the current thread to sleep for <i>secs</i> seconds.</p>
<p><b>See also</b> <a href="qthread.html#msleep">msleep</a>() and
<a href="qthread.html#usleep">usleep</a>().</p>


<h3 class="fn"><a name="stackSize" />int QThread.stackSize (<i>self</i>)</h3><p>Returns the maximum stack size for the thread (if set with
<a href="qthread.html#setStackSize">setStackSize</a>()); otherwise
returns zero.</p>
<p><b>See also</b> <a href="qthread.html#setStackSize">setStackSize</a>().</p>


<h3 class="fn"><a name="start" />QThread.start (<i>self</i>, <a href="qthread.html#Priority-enum">Priority</a>&#160;<i>priority</i>&#160;=&#160;QThread.InheritPriority)</h3><p>This method is also a Qt slot with the C++ signature <tt>void start(QThread::Priority = QThread.InheritPriority)</tt>.</p><p>Begins execution of the thread by calling <a href="qthread.html#run">run</a>(), which should be reimplemented in a
<a href="qthread.html">QThread</a> subclass to contain your code.
The operating system will schedule the thread according to the
<i>priority</i> parameter. If the thread is already running, this
function does nothing.</p>
<p>The effect of the <i>priority</i> parameter is dependent on the
operating system's scheduling policy. In particular, the
<i>priority</i> will be ignored on systems that do not support
thread priorities (such as on Linux, see
http://linux.die.net/man/2/sched_setscheduler for more
details).</p>
<p><b>See also</b> <a href="qthread.html#run">run</a>() and
<a href="qthread.html#terminate">terminate</a>().</p>


<h3 class="fn"><a name="terminate" />QThread.terminate (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void terminate()</tt>.</p><p>Terminates the execution of the thread. The thread may or may
not be terminated immediately, depending on the operating systems
scheduling policies. Use <a href="qthread.html#wait">QThread.wait</a>() after terminate() for
synchronous termination.</p>
<p>When the thread is terminated, all threads waiting for the
thread to finish will be woken up.</p>
<p><b>Warning:</b> This function is dangerous and its use is
discouraged. The thread can be terminated at any point in its code
path. Threads can be terminated while modifying data. There is no
chance for the thread to clean up after itself, unlock any held
mutexes, etc. In short, use this function only if absolutely
necessary.</p>
<p>Termination can be explicitly enabled or disabled by calling
<a href="qthread.html#setTerminationEnabled">QThread.setTerminationEnabled</a>().
Calling this function while termination is disabled results in the
termination being deferred, until termination is re-enabled. See
the documentation of <a href="qthread.html#setTerminationEnabled">QThread.setTerminationEnabled</a>()
for more information.</p>
<p><b>See also</b> <a href="qthread.html#setTerminationEnabled">setTerminationEnabled</a>().</p>


<h3 class="fn"><a name="usleep" />QThread.usleep (int)</h3><p>Causes the current thread to sleep for <i>usecs</i>
microseconds.</p>
<p><b>See also</b> <a href="qthread.html#sleep">sleep</a>() and
<a href="qthread.html#msleep">msleep</a>().</p>


<h3 class="fn"><a name="wait" />bool QThread.wait (<i>self</i>, int&#160;<i>msecs</i>&#160;=&#160;ULONG_MAX)</h3><p>Blocks the thread until either of these conditions is met:</p>
<ul>
<li>The thread associated with this <a href="qthread.html">QThread</a> object has finished execution (i.e. when
it returns from <a href="qthread.html#run">run</a>()). This
function will return true if the thread has finished. It also
returns true if the thread has not been started yet.</li>
<li><i>time</i> milliseconds has elapsed. If <i>time</i> is
ULONG_MAX (the default), then the wait will never timeout (the
thread must return from <a href="qthread.html#run">run</a>()). This
function will return false if the wait timed out.</li>
</ul>
<p>This provides similar functionality to the POSIX
<tt>pthread_join()</tt> function.</p>
<p><b>See also</b> <a href="qthread.html#sleep">sleep</a>() and
<a href="qthread.html#terminate">terminate</a>().</p>


<h3 class="fn"><a name="yieldCurrentThread" />QThread.yieldCurrentThread ()</h3><p>Yields execution of the current thread to another runnable
thread, if any. Note that the operating system decides to which
thread to switch.</p>
<hr /><h2>Qt Signal Documentation</h2><h3 class="fn"><a name="finished" />void finished ()</h3><p>This is the default overload of this signal.</p><p>This signal is emitted when the thread has finished
executing.</p>
<p><b>See also</b> <a href="qthread.html#started">started</a>() and
<a href="qthread.html#terminated">terminated</a>().</p>


<h3 class="fn"><a name="started" />void started ()</h3><p>This is the default overload of this signal.</p><p>This signal is emitted when the thread starts executing.</p>
<p><b>See also</b> <a href="qthread.html#finished">finished</a>()
and <a href="qthread.html#terminated">terminated</a>().</p>


<h3 class="fn"><a name="terminated" />void terminated ()</h3><p>This is the default overload of this signal.</p><p>This signal is emitted when the thread is terminated.</p>
<p><b>See also</b> <a href="qthread.html#started">started</a>() and
<a href="qthread.html#finished">finished</a>().</p>


<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;snapshot-4.9.5-9eb6aac99275 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt&#160;4.8.3</td></tr></table></div></address></body></html>